package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.EditorialMaterialsContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.MyContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.EditoriaMaterialsPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.MyPresenter;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.BottomDialog;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/18 9:35
 * 功能描述： 女孩的话进这个界面 编辑资料
 * 联系方式：1037438704@qq.com
 */


public class EditorialMaterialsActivity extends BaseActivity implements EditorialMaterialsContract, MyContract {

    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.iamge_hidden)
    ImageView iamgeHidden;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.text_preservation)
    TextView textPreservation;
    @BindView(R.id.text_birthday)
    TextView textBirthday;
    @BindView(R.id.text_piease_choose)
    TextView textPieaseChoose;
    @BindView(R.id.text_city)
    TextView textCity;
    @BindView(R.id.edit_tallage)
    EditText editTallage;
    @BindView(R.id.edit_wx)
    EditText editWx;
    @BindView(R.id.edit_qq)
    EditText editQq;
    @BindView(R.id.edit_height)
    EditText editHeight;
    @BindView(R.id.edit_weight)
    EditText editWeight;
    @BindView(R.id.edit_description)
    EditText editDescription;

    private MyPresenter myPresenter;
    private PopupWindow window;
    private View contentView;
    private EditoriaMaterialsPresenter editoriaMaterialsPresenter;
    private String hidden = "1";
    private String id, cityId;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_editorial_materials;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        int count = getIntent().getExtras().getInt("count");
        if (count == 2) {
            textRight.setText("保存");
            textRight.setVisibility(View.VISIBLE);
            textPreservation.setVisibility(View.GONE);
            myPresenter = new MyPresenter(this);
            myPresenter.getMemberBaseData(token, this);
        }
        imageleftFinish.setVisibility(View.VISIBLE);
        textCenter.setText("编辑资料");
        editoriaMaterialsPresenter = new EditoriaMaterialsPresenter(this, this);
//        editoriaMaterialsPresenter.getIndex();
    }

    @OnClick({R.id.image_left_finish, R.id.text_preservation, R.id.text_birthday, R.id.text_piease_choose
            , R.id.text_city, R.id.text_right, R.id.iamge_hidden})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_preservation:
                //保存界面
                editoriaMaterialsPresenter.getModBaseData(token, editTallage.getText().toString().trim(),
                        textCity.getText().toString().trim(),
                        textBirthday.getText().toString(),
                        id,
                        editQq.getText().toString().trim(),
                        editWx.getText().toString().trim(),
                        editHeight.getText().toString().trim(),
                        editWeight.getText().toString(),
                        editDescription.getText().toString(),
                        hidden);
                break;
            case R.id.text_birthday:
                //时间选择器
                TimePickerView pvTime = new TimePickerBuilder(EditorialMaterialsActivity.this,
                        new OnTimeSelectListener() {
                            @Override
                            public void onTimeSelect(Date date, View v) {
                                textBirthday.setText(getTime(date));
                            }
                        }).setTitleText("日期选择")
                        .setSubmitText("确定")//确定按钮文字
                        .setCancelText("取消")//取消按钮文字
                        .setCancelColor(Color.GRAY)
                        .setSubmitColor(Color.RED)
                        .build();
                pvTime.show();
                break;
            case R.id.text_piease_choose:
//                Intent intent = new Intent(this, BottomDialog.class);
//                intent.putExtra("count", "1");
//                startActivity(intent);
                BottomDialog.start(this, "1", 2);
                break;
            case R.id.text_city:
                startActivityForResult(new Intent(this, CityListSelectActivity.class), 1);
                //此处的requestCode应与下面结果处理函中调用的requestCode一致
                break;
            case R.id.text_right:
                //保存界面
                editoriaMaterialsPresenter.getModBaseData(token, editTallage.getText().toString().trim(),
                        textCity.getText().toString().trim(),
                        textBirthday.getText().toString(),
                        id,
                        editQq.getText().toString().trim(),
                        editWx.getText().toString().trim(),
                        editHeight.getText().toString().trim(),
                        editWeight.getText().toString(),
                        editDescription.getText().toString(),
                        hidden);
                break;
            case R.id.iamge_hidden:
                //对他人隐藏我的社交账号 1否 2是
                if (hidden.equals("1")) {
                    iamgeHidden.setImageResource(R.mipmap.icon_voice_true);
                    hidden = "2";
                } else {
                    iamgeHidden.setImageResource(R.mipmap.icon_voice_broadcast_declaration);
                    hidden = "1";
                }
                break;
            default:
        }
    }

    /**
     * 时间选择器
     */
    private String getTime(Date date) {
        //可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
//        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
        return format.format(date);
    }


    public static void start(Context context, int count) {
//        1男的认证    2 我的界面编辑资料
        Bundle bundle = new Bundle();
        bundle.putInt("count", count);
        Intent intent = new Intent(context, EditorialMaterialsActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public void loginSuccess(List<Map<String, String>> data) {

    }

    @Override
    public void loginFailed(String msg) {

    }

    @Override
    public void MySuccess(Map<String, String> data) {
        //获取个人资料

        editTallage.setText(data.get("nickname"));
        textCity.setText(data.get("city_name"));
        textBirthday.setText(data.get("birthday"));
        textPieaseChoose.setText(data.get("expect_ids_name"));
        editWx.setText(data.get("wx"));
        editQq.setText(data.get("qq"));
        id = data.get("expect_ids");


//        "set_hidden_account": "1",// 对他人隐藏我的社交账号 1否 2是
        hidden = data.get("set_hidden_account");
        //对他人隐藏我的社交账号 1否 2是
        if (hidden.equals("1")) {
            iamgeHidden.setImageResource(R.mipmap.icon_voice_broadcast_declaration);
        } else {
            iamgeHidden.setImageResource(R.mipmap.icon_voice_true);
        }
        editHeight.setText(data.get("height"));
        editWeight.setText(data.get("weight"));
        editDescription.setText(data.get("description"));
    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "数据加载中");
    }

    @Override
    public void recovery() {

    }

    //结果处理函数，当从secondActivity中返回时调用此函数
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String city = bundle.getString("city");
            cityId = bundle.getString("id");
            textCity.setText(city);
        }
        if (requestCode == 2 && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            id = bundle.getString("id");
            String name = bundle.getString("name");
            textPieaseChoose.setText(name);
        }
    }

}